/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.controller;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tutwiz.model.Comment;
import net.tutwiz.persistence.CommentPersistenceManager;
import net.tutwiz.persistence.StepPersistenceManager;

/**
 *
 * @author viktor
 */
public class CommentController extends Controller {

//    @EJB
    private CommentPersistenceManager commentPM = new CommentPersistenceManager();
    private StepPersistenceManager stepPM = new StepPersistenceManager();

    public CommentController(String[] params, HttpServletRequest request, HttpServletResponse response) {
        super(params, request, response);
    }

	// Anropar metoden för den action som ska köras
    @Override
    public void doAction() {
        if ("".equals(this.action)) {
            // Default action
            this.all();
        } else if ("all".equals(this.action)) {
            this.all();
        } else if ("show".equals(this.action)) {
            this.show();
        }else if ("allforstep".equals(this.action)) {
            this.allForStep();
        }
    }

    private void allForStep() {
        if (parameters.length > 0) {
            int stepID;
        try {
                stepID = Integer.parseInt(parameters[0]);
            } catch (Exception ex) {
                stepID = -1;
            }
        if (stepID > 0) {
            List<Comment> comments = (List<Comment>) stepPM.get(Long.valueOf(stepID)).getComments();
               // Comment comment = commentPM.get(Long.valueOf(id));
                this.render(comments);
            } else {
                this.niceDie("Comment ID must be an integer!", 400);
            }
        }else {
            this.niceDie("Comment ID missing!", 400);
        }
}
     private void all() {
          List<Comment> comments = commentPM.getAll();
        this.render(comments);
     }
    
    
    private void show() {
        if (parameters.length > 0) {
            int id;
            try {
                id = Integer.parseInt(parameters[0]);
            } catch (Exception ex) {
                id = -1;
            }
            if (id > 0) {
                Comment comment = commentPM.get(Long.valueOf(id));
                this.render(comment);
            } else {
                this.niceDie("Comment ID must be an integer!", 400);
            }
        } else {
            this.niceDie("Comment ID missing!", 400);
        }
    }
}
